var browseHistory = new Array();

$(function() {
	var str = ' ';
	//浏览记录查询
	MuiAjaxFn("client/record/selectrecord?page=1&pagesize=100", {
		"sorting": "commodity_number",
		"tosort": "asc"
	}, function(data) {
		console.log(data);
		if(data.pageInfo.size != 0) {
			//如果有浏览记录
			
			browseHistory = new Array();
			$.each(data.pageInfo.list,function(hidx,hobj){
				var commoditynumber = hobj.commodityNumber

				mui.getJSON(URL_all + "api/bizcommodity/selectbynumber?adcode=100000&commoditynumber="+commoditynumber, {}, function(result) {
					browseHistory.push(result)
					
				});
			})

			setTimeout(function(){
				$.each(browseHistory, function(idx, obj) {
					
					if(obj.htmlState.error){
						//商品不存在也是200  emmm???
					}else{
						var resultobj = obj.dataResultObj
						str += '<div class="p2box-6" data-cNum="' + resultobj.commodityNumber + '"><div class="pimg" style="background-image:url('+IMGUrl+''+resultobj.mainPhoto+')  ;"></div><p>'+resultobj.title+'</p><input type="button" value="￥' + (resultobj.presentPrice / 100) + '" /></div>';
					}
					
				})
				$(".p2box ").html(str);
				$(".p2box .p2box-6:odd").addClass('fleft');
					$(".p2box .p2box-6").on("tap", function() {
					window.top.location.href = 'ProductDetail.html?commodityNumber=' + $(this).attr("data-cNum");
				})
			},1000)
			
			
			
		
			

			//清空全部浏览记录
			$(".DELhistory").attr("data-Uid", data.pageInfo.list.userId);
			$(".DELhistory").on('tap', function() {
				var btnArray = ['取消', '清空'];
				mui.confirm('是否清空浏览记录', '华胜集团', btnArray, function(e) {
					if(e.index == 1) {
						MuiAjaxFn("client/record/deletebyuserid", {
							'userId': $(".DELhistory").attr("data-Uid")
						}, function(data) {
							if(data.dataResultObj.status == 200) {
								mui.toast("清空记录成功");
								nodata('您还没有查看任何一件商品哦!', ".app-cont", 1); //此处有加"猜你喜欢"板块
							}
						})

					} else {

					}
				})

			})
		} else {
			//如果没有浏览记录 
			nodata('您还没有查看任何一件商品哦!', ".app-cont", 1); //此处有加"猜你喜欢"板块
		}
	});

})


function getImgUrlFn(commoditynumber){
	return mui.getJSON(URL_all + "api/bizcommodity/selectbynumber?adcode=100000&commoditynumber="+commoditynumber, {}, function(result) {
		//查询单个商品详情 补全信息
//		console.log(result.dataResultObj.mainPhoto)
//		$(".p2box .p2box-6:eq(" + i + ") .pimg").css({
//			"background-image": "url(" + IMGUrl + "" + result.dataResultObj.mainPhoto + ") "
//		});
//		$(".p2box .p2box-6:eq(" + i + ") p").html(result.dataResultObj.title)
		return result;
		
	});
}
